package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.module.third_server.entity.NginxEntity;
import cn.xenosp.server.module.third_server.service.NginxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 项目名： nginx-web
 * 包路径： cn.xenosp.nginx.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-07-26 23:24
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("/api")
public class NginxController {
    @Autowired
    NginxService nginxService;

    @GetMapping("/list")
    public Result list(Integer page, Integer limit){
        try {
            Map<String, Object> map = nginxService.list(page, limit);
            return Result.OK("",map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }


    @PostMapping("/add")
    public Result add(@RequestBody NginxEntity nginxEntity){
        try {
            return nginxService.add(nginxEntity);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody NginxEntity nginxEntity){
        try {
             nginxService.edit(nginxEntity);
             return Result.OK("","");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }

    @GetMapping("/del/{id}")
    public Result del(@PathVariable("id") String id){
        try {
            nginxService.del(id);
            return Result.OK("","");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }


    @PostMapping("/exec")
    public Result exec(@RequestBody Map<String,Object> map){
        try {
            NginxEntity nginxEntity =  new NginxEntity((Map) map.get("nginx"));
            String exec = (String) map.get("exec");
            nginxService.exec(exec,nginxEntity);
            return Result.OK("","");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }

    @PostMapping("/savefile")
    public Result savefile(@RequestBody Map<String,String> map){
        try {
            String id = (String) map.get("id");
            String value = (String) map.get("value");
            String type = (String) map.get("type");
            nginxService.savefile(id,value,type);
            return Result.OK("","");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误","");
        }
    }
}
